package com.hc.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hc.common.bean.PageBean;
import com.hc.common.utils.ControllerUtil;
import com.hc.domain.Comment;
import com.hc.domain.User;
import com.hc.mapper.CommentMapper;
import com.hc.query.CommentVO;
import com.hc.query.SearchVO;
import com.hc.query.UserVO;
import com.hc.res.ProjsDTO;
import com.hc.res.UserDTO;
import com.hc.service.CommentService;
import com.hc.service.ProjService;
import com.hc.service.UserService;
import com.hc.trans.UserTrans;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 舞遍邓城一条街
 * @description
 * @Date 2021/11/28 15:33
 */
@Controller
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;
    @Resource
    private ProjService projService;
    @Resource
    private UserService userService;


    @PostMapping("/addComment")
    public String addComment(Comment comment,
                             Long projId,
                             HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        comment.setUserId(loginUser.getId());
        comment.setProjId(projId);
        commentService.insert(comment);
        return "";
    }

    /**
     * 根据查询条件查询用户信息
     */
    @GetMapping("/getCommentByCondition/{pageNum}/{pageSize}")
    public ModelAndView getUserByCondition(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            SearchVO searchVO,
            ModelAndView mav
           ) {
        CommentVO commentVO=new CommentVO();
        // 查找条件封装
        ControllerUtil.initParam(commentVO, searchVO);
        PageBean<Comment> commentPageBean = commentService.selectPageByCondition(pageNum, pageSize, commentVO);
//        //转换
//        PageBean<UserDTO> userDTOPageBean = UserTrans.MAPPER.user2UserBO(userPageBean);

       // userDTOPageBean.setUrl("user/getUserByCondition");
        commentPageBean.setUrl("comment/getCommentByCondition");
        mav.addObject("pageBean", commentPageBean);
        //返回查询条件
//        mav.addObject("searchValue", searchVO.getSearchValue());
//        mav.addObject("searchKey", searchVO.getSearchKey());
//        mav.addObject("status",s);
        mav.addObject("title", "用户列表");
        mav.setViewName("comment/list");
        return mav;
    }


    @GetMapping("/showComment/{pageNum}/{pageSize}")
    public ModelAndView showComment(@PathVariable("pageNum") Integer pageNum,
                                    @PathVariable("pageSize") Integer pageSize,
                                    ModelAndView modelAndView,
                                    Long projId){
//        Page<Comment> page = PageHelper.startPage(pageNum, pageSize);
        List<Comment> allCommentByProjId = commentService.getAllCommentByProjId(projId);

        List<User> users = new ArrayList<>();
        for (Comment comment: allCommentByProjId) {
            Long userId = comment.getUserId();
            User user = userService.selectByPrimaryKey(userId);
            users.add(user);
        }
        modelAndView.addObject("proj",projService.selectByPrimaryKey(projId));
        modelAndView.addObject("comments",allCommentByProjId);
        modelAndView.addObject("users",users);
        modelAndView.setViewName("proj/comment");
        return modelAndView;
    }
}